#pragma once

#include "../src/Server.hpp"

static const int default_thread_num = 3;// 线程池大小 设置为3是为了保证1个内核分配1个线程，总共4个内核
static const uint16_t default_timeout = 10;// 默认非活跃连接超时时间

class EchoServer
{
private:
    TcpServer _server;
private:
    void OnClosed(const ConnectionPtr& conn)
    {
        INF_LOG("connection: %ld has closed, socket: %d", conn->Id(), conn->TmpFd());
    }
    void OnConnected(const ConnectionPtr& conn)
    {
        INF_LOG("connection: %ld has established, socket: %d", conn->Id(), conn->Fd());
    }
    void OnMessage(const ConnectionPtr& conn, Buffer* buf)
    {
        // INF_LOG("client[%ld] say: %s", conn->Id(), buf->ReadPosition());
        buf->MoveReadOffset(buf->ReadAbleSize());
        // conn->Send("server has recv your message");
        // conn->Close();
    }
public:
    EchoServer(uint16_t port): _server(port)
    {
        _server.SetLoopThreadCount(default_thread_num);
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        _server.EnableIdelRelease(default_timeout);
    }
    void Start()
    {
        _server.Start();
    }
};